﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace testing_thing
{
    public class Inventory
    {
        private List<Item> _items = new List<Item>();


        public void AddItem(Item value)
        {
            _items.Add(value);
        }

        public int Count
        {
            get { return _items.Count; }
        }

        public Item AccessItem(string id)
        {
            foreach (Item itm in _items)
            {
                if (itm.IdentifiedBy(id))
                {
                    return itm;
                }
            }
            return null;
        }

        public Item TakeItem(string id)
        {
            foreach (Item itm in _items)
            {
                if (itm.IdentifiedBy(id))
                {
                    _items.Remove(itm);
                    return itm;
                }

            }
            return null;

        }

        public string ItemList
        {

            get
            {
                String itemtitles = "";
                foreach (Item itm in _items)
                {
                    itemtitles += itm.Title + "\n";
                }
                return itemtitles;
            }
        }

        [TestFixture]
        public class InventoryTests
        {
            [Test]
            public void TestTakeItem()
            {
                Inventory inv = new Inventory();
                Item itm = new Item(new string[] { "Paper", "Parchment" }, false);
                inv.AddItem(itm);
                Assert.AreEqual(1, inv.Count);
                Assert.AreSame(itm, inv.TakeItem("Paper"));
                Assert.AreEqual(0, inv.Count);


            }

            [Test]
            public void FailedTakeTest()
            {
                Inventory inv = new Inventory();
                Item itm = new Item(new string[] { "Paper", "Parchment" }, false);

                inv.AddItem(itm);
                Assert.IsNull(inv.TakeItem("gobadigoop"));
            }

            [Test]
            public void AccessItemTest()
            {
                Inventory inv = new Inventory();
                Item glass = new Item(new string[] { "glass" }, false);
                inv.AddItem(glass);
                Item kb = new Item(new string[] { "keyboard" }, false);
                inv.AddItem(kb);
                Assert.AreEqual(2, inv.Count);
                Assert.AreSame(kb, inv.AccessItem("keyboard"));
                Assert.AreEqual(2, inv.Count);

            }

            [Test]
            public void ItemListTest()
            {
                Inventory inv = new Inventory();
                Item glass = new Item(new string[] { "glass" }, false);
                glass.Title = "Sheet of Glass";
                inv.AddItem(glass);
                
                Item kb = new Item(new string[] { "keyboard" }, false);
                kb.Title = "Keyboard";
                inv.AddItem(kb);

                StringAssert.Contains(glass.Title, inv.ItemList);
 
            }

            [Test]
            public void ConstructorTest()
            {
                Item pen = new Item(new string[] { "pen" }, false);
                Item bag = new Item(new string[] { "bag" }, true);
                Assert.False(pen.hasInventory);
                Assert.True(bag.hasInventory);
            }

            [Test]
            public void LongDescription()
            {
                Item pen = new Item(new string[] { "pen" }, false);
                Item bag = new Item(new string[] { "bag" }, true);
                pen.Title = "Pen";
                pen.Description = "This is a red pen";
                bag.Title = "Bag";
                bag.Description = "This is a brown leather bag that can contain stuff";
                bag.Inventory.AddItem(pen);
                StringAssert.Contains(pen.Title, bag.LongDescription);
                StringAssert.DoesNotContain(pen.Title, bag.Description);
                bag.Inventory.TakeItem("pen");
                StringAssert.DoesNotContain(pen.Title, bag.LongDescription);
                
            }
            [Test]
            public void LocateEntityTest()
            {
                Item pen = new Item(new string[] { "pen" }, false);
                Item bag = new Item(new string[] { "bag" }, true);
                pen.Title = "Pen";
                pen.Description = "This is a red pen";
                bag.Title = "Bag";
                bag.Description = "This is a brown leather bag that can contain stuff";
                bag.Inventory.AddItem(pen);
                Assert.AreSame(pen, pen.LocateEntity("pen"));
                Assert.AreSame(pen, bag.LocateEntity("pen"));
                bag.Inventory.TakeItem("pen");
                Assert.IsNull(bag.LocateEntity("pen"));
            }

            [Test]
            public void LocateWithoutInventoryTest()
            {
                Item pen = new Item(new string[] { "pen" }, false);
                Assert.IsNull(pen.LocateEntity("fred"));
            }


        }
    }
}
